package com.redlight.utils;

import org.apache.commons.lang3.StringEscapeUtils;

import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chao on 2019/1/7.
 */
public class StringUtil extends org.apache.commons.lang3.StringUtils {
    private static final char SEPARATOR = '_';
    private static final String CHARSET_NAME = "UTF-8";

    public StringUtil() {
    }

    public static String getStrByCharcode(int code) {
        return String.valueOf((char)code);
    }

    public static String toString(byte[] bytes) {
        try {
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException var2) {
            return "";
        }
    }

    public static byte[] getBytes(String str) {
        if(str != null) {
            try {
                return str.getBytes("UTF-8");
            } catch (UnsupportedEncodingException var2) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static String[] strToArray(String inputValue, String regex) {
        if(inputValue == null) {
            inputValue = "";
        }

        inputValue = inputValue.trim();
        StringTokenizer sToken = new StringTokenizer(inputValue, regex);
        int arraySize = sToken.countTokens();
        String[] ret = new String[arraySize];

        for(int index = 0; sToken.hasMoreTokens(); ++index) {
            String token = sToken.nextToken().trim();
            ret[index] = token;
        }

        sToken = null;
        return ret;
    }

    public static String toCamelCase(String inputString) {
        return toCamelCase(inputString, false);
    }

    public static String toCamelCase(String inputString, boolean firstCharacterUppercase) {
        if(inputString == null) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            boolean nextUpperCase = false;

            for(int i = 0; i < inputString.length(); ++i) {
                char c = inputString.charAt(i);
                switch(c) {
                    case ' ':
                    case '#':
                    case '$':
                    case '&':
                    case '-':
                    case '/':
                    case '@':
                    case '_':
                        if(sb.length() > 0) {
                            nextUpperCase = true;
                        }
                        break;
                    default:
                        if(nextUpperCase) {
                            sb.append(Character.toUpperCase(c));
                            nextUpperCase = false;
                        } else {
                            sb.append(Character.toLowerCase(c));
                        }
                }
            }

            if(firstCharacterUppercase) {
                sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
            }

            return sb.toString();
        }
    }

    public static String toUnderlineName(String inputString, String separator) {
        if(inputString == null) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            boolean upperCase = false;

            for(int i = 0; i < inputString.length(); ++i) {
                char c = inputString.charAt(i);
                boolean nextUpperCase = true;
                if(i < inputString.length() - 1) {
                    nextUpperCase = Character.isUpperCase(inputString.charAt(i + 1));
                }

                if(i >= 0 && Character.isUpperCase(c)) {
                    if((!upperCase || !nextUpperCase) && i > 0) {
                        sb.append(separator);
                    }

                    upperCase = true;
                } else {
                    upperCase = false;
                }

                sb.append(Character.toLowerCase(c));
            }

            return sb.toString();
        }
    }

    public static String toLowerCaseFirstOne(String s) {
        return Character.isLowerCase(s.charAt(0))?s:Character.toLowerCase(s.charAt(0)) + s.substring(1);
    }

    public static String toUpperCaseFirstOne(String s) {
        return Character.isUpperCase(s.charAt(0))?s:Character.toUpperCase(s.charAt(0)) + s.substring(1);
    }

    public static String string2Unicode(String string) {
        StringBuffer unicode = new StringBuffer();

        for(int i = 0; i < string.length(); ++i) {
            char c = string.charAt(i);
            unicode.append("\\u" + Integer.toHexString(c));
        }

        return unicode.toString();
    }

    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();
        String[] hex = unicode.split("\\\\u");

        for(int i = 1; i < hex.length; ++i) {
            int data = Integer.parseInt(hex[i], 16);
            string.append((char)data);
        }

        return string.toString();
    }

    public static String abbr(String str, int length) {
        if(str == null) {
            return "";
        } else {
            try {
                StringBuilder sb = new StringBuilder();
                int currentLength = 0;
                char[] var4 = replaceHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray();
                int var5 = var4.length;

                for(int var6 = 0; var6 < var5; ++var6) {
                    char c = var4[var6];
                    currentLength += String.valueOf(c).getBytes("GBK").length;
                    if(currentLength > length - 3) {
                        sb.append("...");
                        break;
                    }

                    sb.append(c);
                }

                return sb.toString();
            } catch (UnsupportedEncodingException var8) {
                var8.printStackTrace();
                return "";
            }
        }
    }

    public static String replaceHtml(String html) {
        if(isBlank(html)) {
            return "";
        } else {
            String regEx = "<.+?>";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(html);
            String s = m.replaceAll("");
            return s;
        }
    }

    public static String jsGetVal(String objectString) {
        StringBuilder result = new StringBuilder();
        StringBuilder val = new StringBuilder();
        String[] vals = split(objectString, ".");

        for(int i = 0; i < vals.length; ++i) {
            val.append("." + vals[i]);
            result.append("!" + val.substring(1) + "?'':");
        }

        result.append(val.substring(1));
        return result.toString();
    }

    public static String toHtml(String txt) {
        return txt == null?"":replace(replace(Encodes.escapeHtml(txt), "\n", "<br/>"), "\t", "&nbsp; &nbsp; ");
    }

    public static String decodeToUTF8(String str) {
        if(str == null) {
            return "";
        } else {
            try {
                return new String(str.getBytes("ISO-8859-1"), "UTF-8");
            } catch (Exception var2) {
                return str;
            }
        }
    }

    public static String strToISO(String str) {
        if(str == null) {
            return "";
        } else {
            try {
                return new String(str.getBytes("UTF-8"), "ISO8859-1");
            } catch (Exception var2) {
                return str;
            }
        }
    }

    public static String strToGBK(String str) {
        if(str == null) {
            return "";
        } else {
            try {
                return new String(str.getBytes("ISO-8859-1"), "GBK");
            } catch (Exception var2) {
                return str;
            }
        }
    }

    public static String converToDbParam(String value) {
        String ret = "";
        if(null == value) {
            return ret;
        } else {
            String[] varr = value.split(",");
            String[] var3 = varr;
            int var4 = varr.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String v = var3[var5];
                ret = ret + ",'" + v.trim() + "'";
            }

            return ret.length() > 0?ret.substring(1):ret;
        }
    }
}
